home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / HTString.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  3.0 KB  |  131 lines

  1. /*        Case-independent string comparison        HTString.c
  2. **
  3. **    Original version came with listserv implementation.
  4. **    Version TBL Oct 91 replaces one which modified the strings.
  5. **    02-Dec-91 (JFG) Added stralloccopy and stralloccat
  6. **    23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
  7. **     6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
  8. */
  9. #include <ctype.h>
  10. #include "HTUtils.h"
  11. #include "tcp.h"
  12.  
  13. PUBLIC int WWW_TraceFlag = 0;    /* Global trace flag for ALL W3 code */
  14.  
  15. #ifndef VC
  16. #define VC "unknown"
  17. #endif
  18.  
  19. PUBLIC CONST char * HTLibraryVersion = "2.12 modified"; /* String for help screen etc */
  20.  
  21. /*    Strings of any length
  22. **    ---------------------
  23. */
  24. PUBLIC int strcasecomp ARGS2 (CONST char*,a, CONST char *,b)
  25. {
  26.     CONST char *p =a;
  27.     CONST char *q =b;
  28.     for(p=a, q=b; *p && *q; p++, q++) {
  29.         int diff = TOLOWER(*p) - TOLOWER(*q);
  30.         if (diff) return diff;
  31.     }
  32.     if (*p) return 1;    /* p was longer than q */
  33.     if (*q) return -1;    /* p was shorter than q */
  34.     return 0;        /* Exact match */
  35. }
  36.  
  37.  
  38. /*    With count limit
  39. **    ----------------
  40. */
  41. PUBLIC int strncasecomp ARGS3(CONST char*,a, CONST char *,b, int,n)
  42. {
  43.     CONST char *p =a;
  44.     CONST char *q =b;
  45.     
  46.     for(p=a, q=b;; p++, q++) {
  47.         int diff;
  48.         if (p == a+n) return 0;    /*   Match up to n characters */
  49.         if (!(*p && *q)) return *p - *q;
  50.         diff = TOLOWER(*p) - TOLOWER(*q);
  51.         if (diff) return diff;
  52.     }
  53.     /*NOTREACHED*/
  54. }
  55.  
  56. /*    Allocate a new copy of a string, and returns it
  57. */
  58. PUBLIC char * HTSACopy
  59.   ARGS2 (char **,dest, CONST char *,src)
  60. {
  61.   if (*dest) free(*dest);
  62.   if (! src)
  63.     *dest = NULL;
  64.   else {
  65.     *dest = (char *) malloc (strlen(src) + 1);
  66.     if (*dest == NULL) outofmem(__FILE__, "HTSACopy");
  67.     strcpy (*dest, src);
  68.   }
  69.   return *dest;
  70. }
  71.  
  72. /*    String Allocate and Concatenate
  73. */
  74. PUBLIC char * HTSACat
  75.   ARGS2 (char **,dest, CONST char *,src)
  76. {
  77.   if (src && *src) {
  78.     if (*dest) {
  79.       int length = strlen (*dest);
  80.       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
  81.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  82.       strcpy (*dest + length, src);
  83.     } else {
  84.       *dest = (char *) malloc (strlen(src) + 1);
  85.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  86.       strcpy (*dest, src);
  87.     }
  88.   }
  89.   return *dest;
  90. }
  91.  
  92.  
  93. /*    Find next Field
  94. **    ---------------
  95. **
  96. ** On entry,
  97. **    *pstr    points to a string containig white space separated
  98. **        field, optionlly quoted.
  99. **
  100. ** On exit,
  101. **    *pstr    has been moved to the first delimiter past the
  102. **        field
  103. **        THE STRING HAS BEEN MUTILATED by a 0 terminator
  104. **
  105. **    returns    a pointer to the first field
  106. */
  107. PUBLIC char * HTNextField ARGS1(char **, pstr)
  108. {
  109.     char * p = *pstr;
  110.     char * start;            /* start of field */
  111.     
  112.     while(*p && WHITE(*p)) p++;        /* Strip white space */
  113.     if (!*p) {
  114.     *pstr = p;
  115.         return NULL;        /* No first field */
  116.     }
  117.     if (*p == '"') {            /* quoted field */
  118.         p++;
  119.     start = p;
  120.     for(;*p && *p!='"'; p++) {
  121.         if (*p == '\\' && p[1]) p++;    /* Skip escaped chars */
  122.     }
  123.     } else {
  124.     start = p;
  125.     while(*p && !WHITE(*p)) p++;    /* Skip first field */
  126.     }
  127.     if (*p) *p++ = 0;
  128.     *pstr = p;
  129.     return start;
  130. }
  131.